/*------------------------------------------------------------------------
//                          Project AttachNetwork                       //
------------------------------------------------------------------------*/
#pragma once
#include <map>

enum {E_MAX_PACKET_POOL=1024};

class IocpPacket
{	
public:
	IocpPacket();
	~IocpPacket();

	void Clear();
	void SetPacket(unsigned long type, BYTE* pPacket, const size_t& size);
	void DeletePacket();
	void SetUse(bool bUse) {m_bUse=bUse;}
	bool IsUse() {return m_bUse;}

	IocpPacket* m_pNext;	
	BYTE* m_pPacket;	
	bool m_bUse;
	size_t m_size;
	DWORD m_Index;


	//appendix	
	unsigned int m_startPos;
	BYTE* GetRawBuffer()
	{
		return m_pPacket;
	}

	unsigned int GetRawSize()
	{
		return m_size;
	}
};

class IoPacketPool
{
public:
	enum 
	{
		EACH_BUF_SIZE=10240
	};

	class PoolList
	{
	public:
		PoolList()
			:m_pBuffer(NULL)
		{}
		~PoolList(){}

		IocpPacket* m_pBuffer;
		CriticalObj m_lock;
	};

	IoPacketPool();
	virtual ~IoPacketPool();

	void Create();
	void Destroy();

	void FreeAll();
	IocpPacket* Alloc();
	IocpPacket* AllocBuffer();
	void	Free(IocpPacket* pIocpPacket);

	PoolList g_List[E_MAX_PACKET_POOL];
	unsigned long g_allocSize;
	unsigned long g_deallocPos;


	IocpPacket* GetRecvBuffer();
	void ReleaseBuffer(IocpPacket* pIocpPacket);
};


class IocpPacket;
namespace PacketAlloc
{
	__declspec(selectany) LONG g_poolIDX=0;
	__declspec(selectany) CriticalObj g_lock;
	typedef std::map<LONG, IocpPacket*> mapIocpPacket;
	__declspec(selectany) mapIocpPacket g_mapList;

	void AddCheckList(IocpPacket* pIocpPacket);
	void PopCheckList(IocpPacket* pIocpPacket);	
	void CheckList();
}